package com.gsc.demo.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * file description
 * Created by 72038967 on 2019/8/14.
 */
@Repository
public class UserRedisImp implements IRedisDao {


    @Autowired
    private RedisTemplate mRedisTemplates;


    public boolean saveObjcetByKey(String key, String values) {
        try {

            mRedisTemplates.opsForValue().set(key, values);
            return true;
        } catch (Exception e) {
            System.out.println("写入失败e"+e.getMessage());
            return false;
        }


    }

    public String getValueByKey(String key) {


        return (String) mRedisTemplates.opsForValue().get(key);

    }

    public boolean saveList(String key, List list) {

        try {
            Long aLong = mRedisTemplates.opsForList().rightPush(key, list);
            System.out.println("存入" + aLong);

            if (aLong > 0) {
                return true;
            }
            return false;


        } catch (Exception e) {
            System.out.println("存入异常" + e.getMessage());
            return false;
        }


    }

    public List getBykey(String key) {

        try {
            return (List) mRedisTemplates.opsForList().rightPop(key);

        } catch (Exception e) {
            System.out.println("读取异常：" + e.getMessage());
            return null;
        }

    }
}
